export default class SimpleRecorder {
    private mediaRecorder: MediaRecorder;
    private stream: MediaStream;
 
    constructor() {
        // 获取音频流
        navigator.mediaDevices.getUserMedia({ audio: true })
            .then(stream => {
                this.stream = stream;
                this.mediaRecorder = new MediaRecorder(stream);
 
                // 处理录音数据
                this.mediaRecorder.ondataavailable = e => {
                    console.log('录音数据:', e.data);
                };
            })
            .catch(error => console.error('获取音频流失败:', error));
    }
 
    startRecording() {
        if (this.mediaRecorder) {
            this.mediaRecorder.start();
            console.log('录音开始');
        }
    }
 
    stopRecording() {
        if (this.mediaRecorder) {
            this.mediaRecorder.stop();
            this.stream.getTracks().forEach(track => {
                console.log('track', track)
                track.stop()
            });
            console.log('录音结束');
        }
    }
 
    // 将录音数据导出为Blob文件并下载
    exportRecording() {
        // if (this.mediaRecorder && this.mediaRecorder.state === 'recording') {
        //     this.mediaRecorder.stop();
        //     console.log('正在导出录音...');
        // }
 
        // setTimeout(() => {
        //     const blob = new Blob(this.mediaRecorder.output);
        //     const url = URL.createObjectURL(blob);
        //     const a = document.createElement('a');
        //     a.href = url;
        //     a.download = 'recording.webm';
        //     a.click();
        //     URL.revokeObjectURL(url);
        // }, 100);
    }
}